//
//  YFBAnimation.swift
//  YFBUIKit
//
//  Created by admin on 2025/9/5.
//

import Foundation
import QuartzCore
import UIKit

public class KBAnimation: NSObject {

    ///抖动效果
    public class func shakeAnimation(_ view: UIView) {
        let shakeAnimation = CABasicAnimation(keyPath: "transform")
        shakeAnimation.duration = 0.08
        shakeAnimation.autoreverses = true
        shakeAnimation.repeatCount = MAXFLOAT
        shakeAnimation.fromValue = NSValue(caTransform3D: CATransform3DRotate(view.layer.transform, -0.1, 0, 0, 1))
        shakeAnimation.toValue = NSValue(caTransform3D: CATransform3DRotate(view.layer.transform, 0.1, 0, 0, 1))
        view.layer.add(shakeAnimation, forKey: "shakeAnimation")
    }
    
    ///停止抖动效果
    public class func stopShakeAnimation(_ view: UIView) {
        view.layer.removeAnimation(forKey: "shakeAnimation")
    }


    ///弹出效果
    public class func showAnimation(_ view: UIView) {
        let animation = CAKeyframeAnimation(keyPath: "transform")
        animation.duration = 0.5
        
        var values = [Any]()
        values.append(NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)))
        values.append(NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)))
        values.append(NSValue(caTransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)))
        values.append(NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)))
        animation.values = values
        
        view.layer.add(animation, forKey: nil)
    }

    
    ///晃动效果-错误提示
    public class func xShakeAnimation(_ view: UIView) {
        let anim = CAKeyframeAnimation(keyPath: "transform.translation.x")
        anim.values = [-5, 0, 5, 0, -5, 0, 5, 0]
        anim.duration = 0.1
        anim.repeatCount = 2
        anim.isRemovedOnCompletion = true
        view.layer.add(anim, forKey:"shake")
    }

}
